package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.DesignModuleDto;
import com.guigu.erp.mapper.DesignModuleMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.DesignModuleDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleDetailsService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Lenovo
 */
@Service
public class DesignModuleServiceImpl extends ServiceImpl<DesignModuleMapper, DesignModule> implements DesignModuleService {
    @Autowired
    private DesignModuleMapper designModuleMapper;

    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;

    @Autowired
    private DesignFileService designFileService;

    /**
     * 获取Id最大物料设计单的设计单编号
     * @return
     */
    @Override
    public String getMaxDesignId() {
        DesignModule designModule = designModuleMapper.queryMaxModule();
        if(designModule==null){
            return null;
        }
        // 获取设计单编号
        String designId = designModule.getDesignId();
        return designId;
    }

    /**
     * 完成物料设计单的制定
     * @param designModuleDto
     */
    @Transactional
    @Override
    public void makeDesignModule(DesignModuleDto designModuleDto) {
        //1.插入物料设计单
        this.save(designModuleDto);
        //2.插入物料设计单明细
        List<DesignModuleDetails> details=designModuleDto.getDetails();
        for (int i=0;i<details.size();i++){
            DesignModuleDetails detail=details.get(i);
            //设置每条明细id为空
            detail.setId(null);
            //获取物料设计单的id
            Integer parentId=designModuleDto.getId();
            //设置父级序号
            detail.setParentId(parentId);
            //设置物料序号
            detail.setDetailsNumber(i+1);
            //设置可用数量
            detail.setResidualAmount(detail.getAmount());
            //插入物料明细
            designModuleDetailsService.save(detail);
        }
        //3.修改产品档案的物料设计标志
        String productId=designModuleDto.getProductId();
        QueryWrapper<DesignFile> wrapper=new QueryWrapper<>();
        wrapper.eq("product_id",productId);
        DesignFile designFile=designFileService.getOne(wrapper);
        designFile.setDesignModuleTag("W001-1");
        designFileService.updateById(designFile);
    }

    @Override
    public PageUtils<DesignModule> page(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignModule> wrapper = new QueryWrapper<DesignModule>();
        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_type())) {
                wrapper.eq("type", vo.getSearch_type());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignModule> designModuleList = this.list(wrapper);
        PageInfo<DesignModule> pageInfo = new PageInfo<DesignModule>(designModuleList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignModule> pageUtils = new PageUtils<DesignModule>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    /**
     * 按条件分页查询物料组成设计的未复核
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<DesignModule> recheckPage2(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<DesignModule> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("first_kind_id",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("thirdKindId",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_type())){
                queryWrapper.eq("type",vo.getSearch_type());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("register_time",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("register_time",vo.getSearch_registerTime2());
            }
        }
        // 额外添加3个条件
        // 审核状态：审核未通过
        queryWrapper.eq("check_tag","S001-0");
        // 删除状态：未删除
        queryWrapper.eq("change_tag","B002-0");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<DesignModule> designModuleList =this.list(queryWrapper);
        PageInfo<DesignModule> designModulePageInfo = new PageInfo<>(designModuleList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignModule> pageUtils = new PageUtils<>();
        pageUtils.setRows(designModulePageInfo.getList());
        pageUtils.setTotal(designModulePageInfo.getTotal());

        return pageUtils;
    }


    /**
     * 物料设计单查询
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<DesignModule> moduleListPage(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignModule> wrapper = new QueryWrapper<DesignModule>();
        //审核状态：审核通过
        wrapper.eq("check_Tag", "S001-1");
        //变更状态: 未变更
        wrapper.eq("change_Tag","B002-0");
        if(vo != null) {
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<DesignModule> designModules = this.list(wrapper);
        PageInfo<DesignModule> pageInfo = new PageInfo<DesignModule>(designModules);
        //PageInfo转PageUtils
        PageUtils<DesignModule> pageUtils = new PageUtils<DesignModule>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }
}
